package com.msn.sign;


import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Build.VERSION;
import android.util.Log;
import android.widget.RemoteViews;

import com.msn.sign.util.ConfigCenter;
import com.msn.sign.util.Constant;
import com.msn.sign.util.DateUtil;
import com.msn.sign.util.SignSearch;

public class SignWidget extends AppWidgetProvider {
	
	private static long lastDay = 0;
	
	private static List<String> signList;	
	
	private static int index = 0;
	
	private static RemoteViews views;
	
	private static Timer timer;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
            Log.i("ID", ""+i+":"+appWidgetId);
            updateAppWidget(context, appWidgetManager,appWidgetId);
            
            //if(Integer.valueOf(VERSION.SDK)>3){
           	 if(timer == null){
                	timer = new Timer();                 	
                	timer.schedule(new WidgetTask(context, appWidgetManager,appWidgetId), 1000, 6000);
                }
         //  }
        }
       
	}

	public static void updateAppWidget(Context context,
			AppWidgetManager appWidgetManager,int appWidgetId) {
		
		long curDay = Long.valueOf(DateUtil.getCurrentDate("yyyyMMdd"));
		if(signList == null || curDay-lastDay>0){
			searchSign();
		}
		
		if(views == null){
			// 创建一个Intent来启动 ExampleActivity
			views = new RemoteViews(context.getPackageName(),R.layout.my_widget);		
			Intent intent = new Intent(context, MainActivity.class);
			PendingIntent pendingIntent =PendingIntent.getActivity(context, 0, intent, 0);
			views.setOnClickPendingIntent(R.id.my_widget, pendingIntent);
		}
				
		if(index>=10){
			index = 0;
		}
		String curContent = signList.get(index);
		if(curContent.length()>40){
			curContent = curContent.substring(0, 40)+"...";
		}
		views.setTextViewText(R.id.sign, curContent);
		
		index++;
		appWidgetManager.updateAppWidget(appWidgetId, views);
	}
	
	private static void searchSign(){
		List<String> tempList = null;
		try {
			tempList = SignSearch.search(DateUtil.getCurrentDate("yyyy/MM/dd"));
		} catch (IOException e) {
			
		}
		if(tempList!=null){
			signList = tempList;
			lastDay = Long.valueOf(DateUtil.getCurrentDate("yyyyMMdd"));
		}
		
	}
	
	@Override
	public void onEnabled(Context context) {
		ConfigCenter.setValue(context, Constant.KEY_WIDGET_ADDED, true);		
		super.onEnabled(context);
	}
	
	@Override
	public void onDisabled(Context context) {
		ConfigCenter.setValue(context, Constant.KEY_WIDGET_ADDED, false);
		if(timer !=null){
			timer.cancel();
			timer = null;
		}
		super.onDisabled(context);
	}
	
	class WidgetTask extends TimerTask{
		
		private Context context;
		
		private AppWidgetManager appWidgetManager;
		
		private int appWidgetId;
		
		public WidgetTask(Context context,
				AppWidgetManager appWidgetManager,int appWidgetId){
			this.context = context;
			this.appWidgetManager = appWidgetManager;
			this.appWidgetId = appWidgetId;
		}
		
		@Override
		public void run() {			
			updateAppWidget(context, appWidgetManager,appWidgetId);
		}
		
	}

}
